//
//  MusicManager.m
//  Slice
//
//  Created by lanou on 16/6/20.
//  Copyright © 2016年 wangjinchao. All rights reserved.
//

#import "MusicManager.h"
static MusicManager *manager = nil;
@implementation MusicManager
{
//    当前的播放对象
    NSString *str;
//    创建定时器不断获取当前的播放进度
    NSTimer *timer;
}

//获取播放器对象
+(instancetype) share {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[MusicManager alloc] init];
    });
    return manager;
}

//重写init方法
- (instancetype)init {
    self = [super init];
    if (self) {
        self.player = [[AVPlayer alloc] init];
        timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
//        加入到runLoop
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    }
    return self;
}
//定时器的方法
- (void)timeAction {
//    判断
    if (self.player.currentItem.duration.timescale && self.player.currentTime.timescale) {
        //获取总时间
        CGFloat totalTime = self.player.currentItem.duration.value / self.player.currentItem.duration.timescale;
        //    获取当前时间
        CGFloat currentTime = self.player.currentTime.value / self.player.currentTime.timescale;
        //   判断是否实现代理方法
        if ([_delgate respondsToSelector:@selector(currentTime:totalTime:)]) {
            //实现了 就调用代理方法
            [_delgate currentTime:currentTime totalTime:totalTime];
        }
    }
  }

//播放某个对象
- (void) playMusicWithStringURL:(NSString *)stringURL {
    
//    判断这个是不是正在播放
    if ([str isEqualToString:stringURL]) {
        return;
    }
    str = stringURL;
//    暂停定时器
    [timer setFireDate:[NSDate distantFuture]];
    
//    先移除就得观察者
    if (self.item != nil) {
        [self.item removeObserver:self forKeyPath:@"status"];
    }
 
    self.item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:stringURL]];
    
    [self.player replaceCurrentItemWithPlayerItem:self.item];
    
//    添加观察者 观察缓存状态
    [self.item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil ];
}
//观察者的回调方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
//    判断当前的缓存状态
    AVPlayer *player = object;
    switch (player.status) {
        case AVPlayerStatusReadyToPlay: {
            [self play12];
           break;
        }
        case AVPlayerStatusFailed: {
            NSLog(@"缓存失败");
            break;
        }
        default:
            NSLog(@"状态未知");
            break;
    }
}

//播放的方法
- (void)play12 {
    [self.player play];
    [timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:0]];
}
//暂停播放
-(void)pause12 {
    [self.player pause];
    [timer setFireDate:[NSDate distantFuture]];
}

- (BOOL)isPlaying {
    if (self.player.rate == 1.0) {
        self.isPlaying = YES;
        return YES;
    } else {
        self.isPlaying = NO;
        return NO;
    }
}

@end
